package cn.pdx.elec.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;


import cn.pdx.elec.dao.ElecUserDao;

import cn.pdx.elec.domain.ElecUser;

/**
 *  @Repository 、@Service 和 @Controller 
     在目前的 Spring 版本中，这 3 个注释和 @Component 是等效的，但是从注释类的命名上，3 个注释分别和持久层 、业务层 和控制层 相对应。
* @author 彭定旭
 *
 */
//或者这样@Repository("cn.pdx.elec.dao.impl.ElecTextDaoImpl")

@Repository(ElecUserDao.SERVICE_NAME)
public class ElecUserDaoImpl extends CommonDaoImpl<ElecUser> implements ElecUserDao {

	
	/**
	 * @Name:findElecPopedomByLogonName
	 * @Description:使用登录名获取当前用户的权限,查询数据库表
	 *                                  elec_user
	 *                                  elec_user_role
	 *                                  elec_user_popedom
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-22(创建日期)
	 * @parameters:String name  登录名
	 * @return:List<Object> 存放用户权限集合
	 */
	public List<Object> findElecPopedomByLogonName(final String name) {
		
		final String sql="select a.popedomcode as popedom  from elec_role_popedom a " +
					"left outer join elec_user_role b on a.roleID = b.roleID " +
					"inner join elec_user c on b.userID=c.userID " +
					"and c.logonName= ? " +
					"where c.isDuty= '1'";
		
		System.out.println("77777777777777777="+sql);
		@SuppressWarnings("unchecked")
		List<Object> list=(List<Object>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				
				Query query=session.createSQLQuery(sql)
				                    .addScalar("popedom",Hibernate.STRING);
				query.setParameter(0, name);
				return query.list();
			}
		});
		return list;
	}

	/**
	 * @Name:findeElecRoleByLogonName
	 * @Description:使用登录名获取当前用户的角色,查询数据库表
	 *                                  elec_user
	 *                                  elec_user_role
	 *                                  elec_user_popedom
	 *                                  elec_ddlSystem
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-4-22(创建日期)
	 * @parameters:String name  登录名
	 * @return:List<Object> 存放用户角色
	 */
	public List<Object []> findeElecRoleByLogonName(final String name) {
		
		final String sql="select b.ddlCode as code,b.ddlName as name from elec_user_role a " +
					"left outer join elec_systemddl b on a.roleID=b.ddlCode " +
					"and b.keyword='角色类型' " +
					"inner join elec_user c on a.userID=c.userID " +
					"and c.logonName=? " +
					"where c.isDuty='1' ";
		
		@SuppressWarnings("unchecked")
		List<Object[]> list=(List<Object[]>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				
				Query query=session.createSQLQuery(sql)
				                    .addScalar("code",Hibernate.STRING)
				                    .addScalar("name",Hibernate.STRING);
				
				query.setParameter(0, name);
				
				return query.list();
			}
		});
		
		return list;
	}

	
}
